package com.xc.ksxt.service;

import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.xc.common.object.PageInfo;
import com.xc.common.tool.BeanAid;
import com.xc.db.Tables;
import java.util.List;
import java.util.concurrent.ExecutionException;
import com.xc.db.tables.Symbol;
import com.xc.db.tables.records.SymbolRecord;
import com.xc.ksxt.entity.SymbolEntity;
import com.xc.ksxt.vo.SymbolVO;
import org.jooq.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SymbolService {
    @Autowired
    private DSLContext dsl;
    private Symbol SYMBOLTB = Symbol.SYMBOL.as("SYMBOLTB");
    public Symbol getTable() {
        return SYMBOLTB;
    }
    public Result<Record> select(Condition whr, PageInfo<SymbolEntity> pageInfo, SortField<?>... orderby) {
        SelectSeekStepN<Record> seek = dsl.select().from(SYMBOLTB).where(whr).orderBy(orderby);
        Result<Record> result = null;
        if (pageInfo != null) {
            SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
            result = su.fetch();
        } else {
            result = seek.fetch();
        }
        if (pageInfo != null) {
            pageInfo.setResult(result);
            pageInfo.setList(result.into(SymbolEntity.class));
        }
        return result;
    }
    public Result<Record> selectVO(Condition whr, PageInfo<SymbolVO> pageInfo, SortField<?>... orderby) {
        SelectSeekStepN<Record> seek = dsl.select().from(SYMBOLTB).where(whr).orderBy(orderby);
        Result<Record> result = null;
        if (pageInfo != null) {
            SelectForUpdateStep<Record> su = seek.limit(pageInfo.getLimit()).offset(pageInfo.getBeginNum());
            result = su.fetch();
        } else {
            result = seek.fetch();
        }
        if (pageInfo != null) {
            pageInfo.setResult(result);
            pageInfo.setList(result.into(SymbolVO.class));
        }
        return result;
    }

    public List<SymbolEntity> select(Condition whr, SortField<?>... orderby) {
        Result<Record> result = dsl.select().from(SYMBOLTB).where(whr).orderBy(orderby).fetch();
        List<SymbolEntity> list = Lists.newArrayList();
        if (result.size() > 0) {
            list = result.into(SymbolEntity.class);
        }
        return list;
    }

    public Long count(Condition cond) {
        Long count = dsl.selectCount().from(SYMBOLTB).where(cond).fetchOneInto(Long.class);
        return count;
    }

    public Record getRecordById(Integer id) {
        Record record = dsl.select().from(SYMBOLTB).where(SYMBOLTB.ID.eq(id)).fetchOne();
        return record;
    }

    public SymbolEntity getEntityById(Integer id) {
        Record r = getRecordById(id);
        if(r==null) {
            return null;
        }
        SymbolEntity entity = r.into(SymbolEntity.class);
        return entity;
    }

    public Integer update(SymbolEntity entity) {
        SymbolRecord record = new SymbolRecord();
        record.from(entity);
        dsl.executeUpdate(record);
        invalidateCache(entity.getId());
        return Integer.valueOf(entity.getId());
    }

    public Integer insert(SymbolEntity entity) {
        SymbolRecord record = new SymbolRecord();
        record.from(entity);
        InsertResultStep<SymbolRecord> su = dsl.insertInto(Tables.SYMBOL).set(record).returning(getTable().ID);
        int id = su.fetchOne().getId();
        entity.setId(id);
        invalidateCache(id);
        return id;
    }

    public Integer save(SymbolEntity entity) {
        if (entity.getId()==null) {
            return insert(entity);
        }
        SymbolEntity entity2 = getEntityById(Integer.valueOf(entity.getId()));
        if(entity2==null) {
            return insert(entity);
        }
        return update(entity);
    }

    public void deleteById(Integer id) {
        dsl.delete(SYMBOLTB).where(SYMBOLTB.ID.eq(id)).execute();
        invalidateCache(id);
    }
    public static LoadingCache<Integer, Optional<SymbolEntity>> cache = CacheBuilder.newBuilder().maximumSize(500).build(
            new CacheLoader<Integer, Optional<SymbolEntity>>() {
                public Optional<SymbolEntity> load(Integer id) {
                    SymbolService service = BeanAid.getBean(SymbolService.class);
                    SymbolEntity user = service.getEntityById(id);
                    return Optional.fromNullable(user);
                }
            });
    public static SymbolEntity getFromCaches(Integer key) {
        if (key == null) {
            return null;
        }
        try {
            return cache.get(key).orNull();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static SymbolEntity getFromCaches(String key) {
        if (key == null) {
            return null;
        }
        try {
            return cache.get(Integer.valueOf(key)).orNull();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void invalidateCache(String id) {
        cache.invalidate(Integer.valueOf(id));
    }
    public static void invalidateCache(Integer id) {
        cache.invalidate(id);
    }
}
